package edu.ucsd.ccdb;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
 
public class SunriseLin1 
{
	static class CollapsiblePanel
		extends JPanel 
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private class TitlePanel
			extends JPanel
		{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			private Color fadeto= new Color(200,212,247);
 
			public TitlePanel(LayoutManager layout) {
				super(layout);
			}
 
			protected void paintComponent(Graphics g) 
			{
				GradientPaint gradient=
					new GradientPaint(
						0, 0, Color.white, 
						getSize().width -10, getSize().height, fadeto,
						true);
 
				Graphics2D g2d = (Graphics2D) g;
				g2d.setPaint(gradient);
				g2d.fill(new Rectangle(getSize().width, getSize().height));
			}
		}
 
		private JPanel mPanel= new JPanel(new GridLayout(0,1,2,2));
		private JLabel mTitle;
		private JLabel mButton;
 
		public CollapsiblePanel(String title)
		{
			setLayout(new BorderLayout());
 
			final Color active= new Color(66,142,255);
			final Color inactive= new Color(33,93,198);
			final ImageIcon up= new ImageIcon("up.gif");
			
			final ImageIcon down= new ImageIcon("down.gif");
 
			mTitle= new JLabel(title);
			mTitle.setForeground(inactive);
			mButton= new JLabel(up);
 
			TitlePanel panel= new TitlePanel(new BorderLayout()); 
			panel.setBorder(new EmptyBorder(2,2,2,2));
			panel.add(mTitle, BorderLayout.CENTER);
			panel.add(mButton, BorderLayout.EAST);
 
			panel.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					mPanel.setVisible(!mPanel.isVisible());
					mButton.setIcon(mPanel.isVisible() ? up : down);
				}
				public void mouseEntered(MouseEvent e) {
					mTitle.setForeground(active);
				}
				public void mouseExited(MouseEvent e) {
					mTitle.setForeground(inactive);
				}
			});
 
			panel.setBackground(Color.WHITE);
 
			mPanel.setBorder(new EmptyBorder(2,2,2,2));
			mPanel.setBackground(new Color(214,223,247));
 
			add(panel, BorderLayout.NORTH);
			add(mPanel, BorderLayout.CENTER);
		}
 
		public Component add(Component comp)
		{
			return mPanel.add(comp);
		}
	}
 
	static class CollapsiblePanelBar extends JPanel 
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private class VerticalLayout
			implements LayoutManager 
		{
			private int vgap;
 
			public VerticalLayout(int vgap) { this.vgap = vgap; }
 
			public void addLayoutComponent(String name, Component comp) {}
 
			public void removeLayoutComponent(Component comp) {}
 
			public Dimension preferredLayoutSize(Container parent) 
			{
				synchronized (parent.getTreeLock()) {
					Insets insets = parent.getInsets();
					int ncomponents = parent.getComponentCount();
					int w = 0;
					int h = 0;
					for (int i = 0 ; i < ncomponents ; i++) {
						Component comp = parent.getComponent(i);
						Dimension d = comp.getPreferredSize();
						if (w < d.width) 
							w= d.width;
						h += d.height;
					}
					return new Dimension(
						insets.left + insets.right + w, 
						insets.top + insets.bottom + h + (ncomponents-1)*vgap);
				}
			}
 
			public Dimension minimumLayoutSize(Container parent) 
			{
				return preferredLayoutSize(parent);
			}
 
			public void layoutContainer(Container parent) 
			{
				synchronized (parent.getTreeLock()) {
					Insets insets = parent.getInsets();
					int ncomponents = parent.getComponentCount();
					int w= parent.getWidth() - (insets.left + insets.right);
					int x= insets.left;
					int y= insets.top;
					for (int i= 0; i< ncomponents; i++) {
						Component comp= parent.getComponent(i);
						comp.setBounds(x, y, w, comp.getPreferredSize().height);
						y += (comp.getPreferredSize().height +vgap);
					}
				}
			}
		}
 
		public CollapsiblePanelBar()
		{
			setLayout(new VerticalLayout(10));
			setBorder(new EmptyBorder(10,10,10,10));
			setBackground(new Color(117,151,226));
		}
 
		public void add(CollapsiblePanel panel)
		{
			super.add(panel);
		}
	}
 
	public static void main(String[] argv)
	{
		CollapsiblePanelBar bar= new CollapsiblePanelBar();
 
		CollapsiblePanel panel= new CollapsiblePanel("Panel 1");
		for (int i= 0; i< 8; i++) 
			panel.add(new JLabel("Panel 1, selection " +(i+1)));
		bar.add(panel);
 
		panel= new CollapsiblePanel("Panel 2");
		for (int i= 0; i< 4; i++) 
			panel.add(new JLabel("Panel 2, selection " +(i+1)));
		panel.add(new JLabel("Panel 2, slightly longer selection"));
		bar.add(panel);
 
		panel= new CollapsiblePanel("Details");
		panel.add(new JLabel(
			"<html>" +
			"<b>BondPrice.java</b><br>" +
			"Java Languange Source File<p>" +
			"Date Modified: 20 August 2003, 08:32<p>" +
			"Size: 602 bytes" +
			"</html>"));
		bar.add(panel);
 
		JScrollPane sp= new JScrollPane(bar);
		sp.setBorder(null);
		sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
 
		JFrame frame= new JFrame("CollapsiblePanelBar");
		frame.getContentPane().add(sp);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
}